package countLatch;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CycliBarrier {
    public static void main(String[] args) throws InterruptedException {
//        //加法计数器
//        CountDownLatch c = new CountDownLatch(6);
//        for (int i = 0; i < 6; i++) {
//            new Thread(()->{
//                System.out.println(Thread.currentThread().getName()+"->go out!");
//                c.countDown();
//            },String.valueOf(i)).start();
//        }
//        c.await();
//        System.out.println("close door");
        //减法计数器
        CyclicBarrier cy = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙成功");
        });
        for (int i = 0; i < 7; i++) {
              final int temp = i;
            new Thread(()->{
                System.out.println("召唤神龙"+ temp);
                try {
                    cy.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
